package com.ruoyi.info.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.health.domain.Family;
import com.ruoyi.health.service.IFamilyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 家人 Controller
 * 
 * @author libin
 * @date 2023-08-21
 */
@RestController
@RequestMapping("/info/healthFamily")
public class FrontFamilyController extends BaseController
{
    @Autowired
    private IFamilyService familyService;

    /**
     * 查询家人 列表
     */
    @GetMapping("/list")
    public TableDataInfo list()
    {
        // startPage();  不分页
        //【获取的是前台用户的 id】
        Long frontUserId = SecurityUtils.getUserId();
        if(StringUtils.isNotNull(frontUserId)){
            List<Family> familyList = familyService.selectFamilyListByUserId(frontUserId);
            return getDataTable(familyList);
        }else{
            return getDataTable(null);
        }

    }


    /**
     * 获取家人 详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getFamilyInfo(@PathVariable Long id)
    {
        //【获取的是前台用户的 id】
        Long frontUserId = SecurityUtils.getUserId();
        if(StringUtils.isNotNull(frontUserId)){
            // 判断 id是不是 frontUserId的家人!
            return AjaxResult.success(familyService.selectFamilyById(id,frontUserId));
        }else{
            return AjaxResult.error("获取家人信息失败！无法获取前台用户信息！");
        }

    }

    /**
     * 新增家人 
     */
    @Log(title = "家人新增", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Family family)
    {
        //【获取的是前台用户的 id】
        Long frontUserId = SecurityUtils.getUserId();
        if(StringUtils.isNotNull(frontUserId)){
            family.setUserId(frontUserId);
            return toAjax(familyService.insertFamily(family));
        }else{
            return AjaxResult.error("新增家人信息失败！无法获取前台用户信息！");
        }

    }

    /**
     * 修改家人 
     */
    @Log(title = "家人修改", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Family family) {
        //【获取的是前台用户的 id】
        Long frontUserId = SecurityUtils.getUserId();
        if(StringUtils.isNotNull(frontUserId)){
            family.setUserId(frontUserId);
            return toAjax(familyService.updateFamily(family));
        }else{
            return AjaxResult.error("修改家人信息失败！无法获取前台用户信息！");
        }

    }

    /**
     * 删除家人 
     */
    @Log(title = "家人删除", businessType = BusinessType.DELETE)
	@DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id)
    {
        //【获取的是前台用户的 id】
        Long frontUserId = SecurityUtils.getUserId();
        if(StringUtils.isNotNull(frontUserId)){
            // TODO:判断 id是不是 frontUserId的家人？
            return toAjax(familyService.deleteFamilyById(id));
        }else{
            return AjaxResult.error("删除家人失败！无法获取前台用户信息！");
        }
    }



}
